import {
  createRouter,
  createWebHashHistory
} from 'vue-router'


const routes = [

  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登陆'
    }
  },
  {
    path: '/Register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')


  },
  {
    path: '/My',
    name: 'My',
    component: () => import('../views/My.vue'),
    meta: {
      title: '我的'
    }
  },
  {
    path: '/Cart',
    name: 'Cart',
    component: () => import('../views/Cart.vue'),
    meta: {
      title: '购物车'
    }
  },
  {
    path: '/Order',
    name: 'Order',
    component: () => import('../views/Order/List.vue'),
    meta: {
      title: '订单'
    }
  },
  {
    path: '/Test',
    name: 'Test',
    component: () => import('../views//Test.vue'),
    meta: {
      title: '测试'
    }
  },
  {
    path: '/OrderSumbit',
    name: 'OrderSumbit',
    component: () => import('../views/Order/OrderSumbit.vue'),
    meta: {
      title: '确认订单'
    }
  },
  {
    path: '/Search',
    name: 'Search',
    component: () => import('../views/Search/Index.vue'),
    meta: {
      title: '搜索'
    }
  },
  {
    path: '/SearchList',
    name: 'SearchList',
    component: () => import('../views/Search/List.vue'),
    meta: {
      title: '搜索列表'
    }
  },
  {
    path: '/Address',
    name: 'Address',
    component: () => import('../views/Address/Index.vue'),
    meta: {
      title: '地址管理'
    }
  },
  {
    path: '/AddressEdit/:id?',
    name: 'AddressEdit',
    component: () => import('../views/Address/Edit.vue'),
    meta: {
      title: '编辑收货地址'
    }
  }, {
    path: '/Shop',
    name: 'Shop',
    component: () => import('../views/Shop/Shop.vue'),
    meta: {
      title: '店铺'
    }
  }

]

const router = createRouter({
  history: createWebHashHistory(),

  routes
})
//路由拦截
router.beforeEach((to, from, next) => {
  const { isLogin } = localStorage;
  
  const { name } = to;
  const isLoginOrRegister = (name === "Login" || name === "Register");
  (isLogin=='true' || isLoginOrRegister) ? next(): next({ name: 'Login' });
})
export default router
